package com.gouaixin.bee.beeframework2.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.util.AbToastUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.MyAPI;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.adapter.ZhenxinhuaAdapter;
import com.gouaixin.bee.beeframework2.bean.ZHENXINHUA;
import com.gouaixin.bee.beeframework2.bean.BaseModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/3/10.
 */
//真心话问题界面
public class B24_zhenxinhuawentiActivity extends FragmentActivity {
    Context context=B24_zhenxinhuawentiActivity.this;

    //顶部
    ImageView back;//返回
    TextView title;//标题
    TextView right;//顶部右边图标



    //内容区
    ListView listView;
    ZhenxinhuaAdapter adapter;
    List<ZHENXINHUA> datas=new ArrayList<ZHENXINHUA>();

    String jid="";
    String cid="";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b24_zhenxinhuawenti);
        Intent it=getIntent();
        jid=it.getStringExtra("jid");
        cid= PreferencesUtils.getString(context,"cid");

        mLog.e("B24_zhenxinhuawentiActivity___jid=" + jid);
        mLog.e("B24_zhenxinhuawentiActivity___cid=" + cid);



        initView();



        setData();

        setListener();
    }

    private void initView() {
        back= (ImageView) findViewById(R.id.back);
        title= (TextView) findViewById(R.id.title);
        right= (TextView) findViewById(R.id.back2);

        listView= (ListView) findViewById(R.id.listview);
        adapter=new ZhenxinhuaAdapter(context);
        listView.setAdapter(adapter);

    }


    private void setData() {
        BaseModel.setCallBackLister(new BaseModel.CallBackLister() {
            @Override
            public String onCallBack(String url, String data) {
                mLog.e("返回的结果：" + data);
                datas = new Gson().fromJson(data, new TypeToken<List<ZHENXINHUA>>() {
                }.getType());

                mLog.e(datas.toString());
                mLog.e(new Gson().toJson(datas));

                adapter.setData(datas);
                adapter.notifyDataSetChanged();


                return url;
            }
        });
        RequestParams params = new RequestParams();
        params.addBodyParameter("id", jid);
        params.addBodyParameter("cid", cid);
        BaseModel.post(MyAPI.getzhenxinhua, params);






//
//
//        ZHENXINHUA bean=new ZHENXINHUA();
//        String wenti="你是好人吗？";
//        bean.setWenti(wenti);
//        String[]daan={"我是好人","我是坏人","解码未来-eoe移动开发系列讲座深圳站，将于2014年5月18日（周日）在深圳大学举行。本次讲座由eoe移动开发者社区主办，为国内最大的移动开发者社区，多年来一直致力于推动移动开发者学习与交流。"};
//        bean.setDaan(daan[0]);
//        mLog.e(new Gson().toJson(bean));


    }





    long oldTime = 0;//记录上次按返回键的时间
    private void setListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                long currentTime = System.currentTimeMillis();

                if (currentTime - oldTime < 3 * 1000) {
                    AbToastUtil.showToast(context,"亲！换的太频繁太累");
                } else {
                    oldTime =  System.currentTimeMillis();;
                    setData();
                }

            }
        });




        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Intent it=new Intent(context,B23_zhenxinhuaActivity.class);
                it.putExtra("wenti",datas.get(position).getWenti());
                it.putExtra("daan",datas.get(position).getDaan());
                setResult(1, it);
                finish();

            }
        });


    }
}
